using System;
using UnityEngine;

namespace ProceduralPrimitivesUtil
{
	public class Torus : PPBase
	{
		[Header("Basic parameters")]
		public float radius1 = 0.5f;

		public float radius2 = 0.1f;

		[Header("Segments")]
		public int sides = 24;

		public int segments = 12;

		[Header("Slice")]
		public bool sliceOn;

		public float sliceFrom;

		public float sliceTo = 360f;

		[Header("Mapping Coordinates")]
		public bool generateMappingCoords = true;

		public bool realWorldMapSize;

		public Vector2 UVOffset = new Vector2(0f, 0f);

		public Vector2 UVTiling = new Vector2(1f, 1f);

		[Header("Others")]
		public bool flipNormals;

		public bool smooth = true;

		protected override void CreateMesh()
		{
			m_mesh.name = "Torus";
			radius1 = Mathf.Clamp(radius1, 0f, 10000f);
			radius2 = Mathf.Clamp(radius2, 0f, 10000f);
			sides = Mathf.Clamp(sides, 3, 100);
			segments = Mathf.Clamp(segments, 3, 100);
			sliceFrom = Mathf.Clamp(sliceFrom, 0f, 360f);
			sliceTo = Mathf.Clamp(sliceTo, sliceFrom, 360f);
			CreateTorus(pivotOffset + Vector3.zero, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius1, radius2, sides, segments, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth, 0f, 360f);
			if (sliceOn)
			{
				Vector3 vector = new Vector3(Mathf.Sin(sliceFrom * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceFrom * (MathF.PI / 180f))) * radius1;
				Vector3 vector2 = new Vector3(Mathf.Sin(sliceTo * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceTo * (MathF.PI / 180f))) * radius1;
				CreateCircle(pivotOffset + m_rotation * vector, m_rotation * -vector.normalized, m_rotation * Vector3.down, radius2, segments, 2, sliceOn: false, 0f, 360f, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals);
				CreateCircle(pivotOffset + m_rotation * vector2, m_rotation * -vector2.normalized, m_rotation * Vector3.up, radius2, segments, 2, sliceOn: false, 0f, 360f, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals);
			}
		}
	}
}
